home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Firefox Backup Extension 6.0.3 / chrome / febe.jar / content / febeZip.js < prev   
Text File  |  2008-08-06  |  7KB  |  217 lines

  1. // author: Chuck Baker
  2. // contact: febe@customsoftwareconsult.com
  3. // Version 6.0
  4.  
  5. function febeZip(sourceDir,destDir,zipName,absolutePath,append){
  6.   // Zip the contents of sourceDir (including all subdirectories) into destDir as zipName
  7.   // If absolutePath is true, include the full pathname for each entry added to zipName
  8.   // If append is false, create a new zipfile, otherwise append to the existing zipfile
  9.   // Initialize
  10.     var root = src = sourceDir;
  11.     var zipW;
  12.     
  13.     const PR_RDONLY      = 0x01
  14.     const PR_WRONLY      = 0x02
  15.     const PR_RDWR        = 0x04
  16.     const PR_CREATE_FILE = 0x08
  17.     const PR_APPEND      = 0x10
  18.     const PR_TRUNCATE    = 0x20
  19.     const PR_SYNC        = 0x40
  20.     const PR_EXCL        = 0x80
  21.     const COMPRESSION_NONE    = 0;
  22.     const COMPRESSION_FASTEST = 1;
  23.     const COMPRESSION_DEFAULT = 6;
  24.     const COMPRESSION_BEST    = 9;
  25.     
  26.     var ZipWriter = Components.Constructor("@mozilla.org/zipwriter;1","nsIZipWriter");
  27.     var ZipReader = Components.Constructor("@mozilla.org/libjar/zip-reader;1","nsIZipReader", "open");
  28.  
  29.     // Init routine
  30.     var init = function (destDir,zipName){
  31.         febeZipFinished = false;    // Set to 'true' when zipping is completed
  32.         var zipFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  33.         zipFile.initWithPath(destDir);
  34.         if(!zipFile.exists() || !zipFile.isDirectory()){
  35.             zipFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  36.         }//if        
  37.         zipFile.append(zipName);
  38.         if (zipFile.exists() && !append){zipFile.remove(true);}
  39.         zipW = new ZipWriter();
  40.         if(append){
  41.             zipW.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_APPEND);
  42.         }else{
  43.             zipW.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
  44.         }
  45.     }//init()
  46.     
  47.     // Define the zip routine
  48.     var zip = function (src){
  49.         var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  50.         if (!aFile) return false;
  51.         aFile.initWithPath(src);
  52.         if(!aFile.exists()){
  53.             var tmp = "febeZip: "+febeMsg[61].replace(/%SRC%/,src);
  54.             febeFatal("FILE NOT FOUND",tmp);
  55.             return talse;
  56.         }//if
  57.         
  58.         var entries = aFile.directoryEntries;
  59.         while(entries.hasMoreElements()){
  60.             var entry = entries.getNext();
  61.             entry.QueryInterface(Ci.nsIFile);
  62.             var src = entry.path;
  63.             if(entry.isDirectory()){
  64.                 zip(src);    //Recurse!
  65.             }else{
  66.                 var file = src.substring(root.length+1);;
  67.                 if(absolutePath) file = src;
  68.                 file = file.replace(/\\/g,"/");
  69.                 
  70.                 // Ignore locked files
  71.                 if(file == "parent.lock"){continue;}
  72.                 if(file == "lock"){continue;}
  73.                 if(file == "places.sqlite-journal"){continue;}
  74.                 if(file == "places.sqlite-stmtjrnl"){continue;}
  75.                 try{
  76.                     zipW.addEntryFile(file, Ci.nsIZipWriter.COMPRESSION_BEST, entry, false);
  77.                 }catch(e){
  78.                     if(e.toString().indexOf("NS_BASE_STREAM_CLOSED") != -1){continue;}
  79.                     if(e.toString().indexOf("NS_ERROR_FILE_ALREADY_EXISTS") != -1){
  80.                         zipW.removeEntry(file, false);
  81.                         zipW.addEntryFile(file, Ci.nsIZipWriter.COMPRESSION_BEST, entry, false);
  82.                         continue;
  83.                     }//if
  84.                     febeSetMsgs();
  85.                     var msg = febeMsg[53].replace(/%FILE%/,file);
  86.                     febeFatal(e,msg);
  87.                 }//try/catch
  88.             }//if
  89.         }//while
  90.     }//zip()
  91.     
  92.     // nsIRequestObserver methods
  93.     var observer = {
  94.         onStartRequest: function(request, context){},
  95.         onStopRequest:  function(request, context, status){
  96.             zipW.close();
  97.             febeZipFinished = true;
  98.         }      
  99.     };
  100.     init(destDir,zipName);
  101.     zip(src);
  102.     zipW.processQueue(observer,null);
  103.     return true;
  104. }// febeZip()
  105.  
  106. function febeUnzip(sourceDir,destDir,zipName){
  107.   // Unzip the contents of zipName (located in sourceDir) into destDir overwriting existing entries
  108.     var zipFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  109.     zipFile.initWithPath(sourceDir);
  110.     zipFile.append(zipName);
  111.     var zipR = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(Ci.nsIZipReader);
  112.     zipR.open(zipFile);
  113.     var entries = zipR.findEntries("*");
  114.     while (entries.hasMore()) {
  115.         var entry = entries.getNext();
  116.         var zEntry = zipR.getEntry(entry)
  117.         var outFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  118.         outFile.initWithPath(destDir);
  119.         var subDirs = entry.split("/");
  120.         var end = subDirs.length;
  121.         if(zEntry.isDirectory){--end;}
  122.         
  123.         for (var i = 0; i < end; ++i) {
  124.           if(!febeIsDrive(subDirs[i])) outFile.append(subDirs[i]);
  125.           if (!(outFile.exists() || febeIsDrive(subDirs[i]))) {
  126.                 outFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  127.             }
  128.         }
  129.  
  130.         if(!zEntry.isDirectory){
  131.           outFile.append(subDirs[end]);
  132.           zipR.extract(entry,outFile);
  133.         }
  134.     }//while
  135.     return true;
  136.   }//febeUnzip()
  137.   
  138. function febeIsDrive(path){
  139.     // Is path a drive disignator?  ("C:", etc)
  140.     if(path.match(":")) return true
  141.     return false;
  142. }//febeIsDrive()
  143.   
  144.   function febeAddToZip(sourceNames,destDir,zipName){
  145.   // Zip the files listed in sourceNames array into destDir as zipName -  sourceNames contains full pathnames of files only.  Directories are ignored, zipName is overwriten
  146.     // Initialize
  147.     //var root = src = sourceDir;
  148.     var zipW;
  149.     
  150.     const PR_RDONLY      = 0x01
  151.     const PR_WRONLY      = 0x02
  152.     const PR_RDWR        = 0x04
  153.     const PR_CREATE_FILE = 0x08
  154.     const PR_APPEND      = 0x10
  155.     const PR_TRUNCATE    = 0x20
  156.     const PR_SYNC        = 0x40
  157.     const PR_EXCL        = 0x80
  158.     const COMPRESSION_NONE    = 0;
  159.     const COMPRESSION_FASTEST = 1;
  160.     const COMPRESSION_DEFAULT = 6;
  161.     const COMPRESSION_BEST    = 9;
  162.     
  163.     var ZipWriter = Components.Constructor("@mozilla.org/zipwriter;1",
  164.                                        "nsIZipWriter");
  165.     var ZipReader = Components.Constructor("@mozilla.org/libjar/zip-reader;1",
  166.                                        "nsIZipReader", "open");
  167.  
  168.     // Init routine
  169.     var init = function (destDir,zipName){
  170.         febeZipFinished = false;    // Set to 'true' when zipping is completed
  171.         var zipFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  172.         zipFile.initWithPath(destDir);
  173.         if(!zipFile.exists() || !zipFile.isDirectory()){
  174.             zipFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  175.         }//if        
  176.         zipFile.append(zipName);
  177.         if (zipFile.exists()){zipFile.remove(true);}
  178.         zipW = new ZipWriter();
  179.         zipW.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
  180.     }//init()
  181.     
  182.     // Define the zip routine
  183.     var zip = function (){
  184.         for(var i in sourceNames){
  185.             var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  186.             if (!aFile) return false;
  187.             var entry = sourceNames[i];
  188.             aFile.initWithPath(entry.path);
  189.             aFile.append(entry.name);
  190.             if(aFile.isDirectory()){
  191.                 continue;
  192.             }else{
  193.                 try{
  194.                     zipW.addEntryFile(entry.name, Ci.nsIZipWriter.COMPRESSION_BEST, aFile, false);
  195.                 }catch(e){
  196.                     var msg = febeMsg[53].replace(/%FILE%/,entry.name)
  197.                     febeFatal(e,msg)
  198.                 }
  199.  
  200.             }//if
  201.         }//for
  202.     }//zip()
  203.     
  204.     // nsIRequestObserver methods
  205.     var observer = {
  206.         onStartRequest: function(request, context){},
  207.         onStopRequest:  function(request, context, status){
  208.             zipW.close();
  209.             //febeUpdateProgressWindow(zipName);
  210.             febeZipFinished = true;
  211.         }      
  212.     };
  213.     init(destDir,zipName);
  214.     zip();
  215.     zipW.processQueue(observer,null);
  216.     return true;
  217. }// febeAddToZip()